package com.enjoy.seata.service.impl;

import com.enjoy.seata.service.AbstractDataCheck;
import com.enjoy.seata.service.StoreService;
import com.enjoy.seata.util.DataSourceUtil;


public class StoreServiceImpl extends AbstractDataCheck implements StoreService {

    public static final String DB_KEY = "stock";

    @Override
    public void deduct(String commodityCode, int count) {
        String sql = "update stock_tbl set count = count - " + count + " where commodity_code = '" + commodityCode + "'";
        DataSourceUtil.executeUpdate(DB_KEY, sql);
    }

    @Override
    public void reset(String key, String value) {
        String deleteSql = "delete from stock_tbl where commodity_code = '" + key + "'";
        String insertSql = "insert into stock_tbl(commodity_code, count) values ('" + key + "', " + value + ")";
        DataSourceUtil.executeUpdate(DB_KEY, deleteSql);
        DataSourceUtil.executeUpdate(DB_KEY, insertSql);
    }

    @Override
    public int doNegativeCheck(String field, String id) {
        String checkSql = "select " + field + " from stock_tbl where commodity_code='" + id + "'";
        String result = DataSourceUtil.getSingleResult(DB_KEY, checkSql);
        return Integer.parseInt(result);

    }
}
